/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.Aval;
import edu.upn.sigemfi.gar.be.Garantia;
import edu.upn.sigemfi.gar.be.GarantiaPrendaria;
import edu.upn.sigemfi.gar.be.GarantiaHipotecaria;
import edu.upn.sigemfi.gar.be.GarantiaWarrant;
import edu.upn.sigemfi.gar.be.GarantiaPignoraticia;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author Programador
 */
@Stateless

public class GarantiaFacade implements GarantiaFacadeLocal {
    //@PersistenceUnit(name="edu.upn.sigemfi.gar-ejbPU")
    @PersistenceContext
    private EntityManager em;

    public void create(Garantia garantia) {
        em.persist(garantia);
    }

    public void edit(Garantia garantia) {
        em.merge(garantia);
    }

    public void remove(Garantia garantia) {
        em.remove(em.merge(garantia));
    }

    public Garantia find(Object id) {
        return em.find(Garantia.class, id);
    }

    public List<Garantia> findAll() {
        return em.createQuery("select object(o) from Garantia as o").getResultList();
    }

    //Garantias Prendarias

    public GarantiaPrendaria findPrendaria(Object id){
        return em.find(GarantiaPrendaria.class, id);
    }

    public List<GarantiaPrendaria> findAllPrendarias() {
        return em.createQuery("select object(o) from GarantiaPrendaria as o").getResultList();
    }

    public List<GarantiaPrendaria> findPrendariasByPropietario(String criterio)
    {
        return em.createQuery("select object(o) from GarantiaPrendaria o where o.propietario like '%"+criterio+"%'").getResultList();
    }

    public List<GarantiaPrendaria> findPrendariasByCliente(String criterio)
    {
        return em.createQuery("select object(o) from GarantiaPrendaria as o inner join o.cliente PersonaNatural where PersonaNatural.nombrePersona like '%"+criterio+"%' or PersonaNatural.apeMaternoPersona like '%"+criterio+"%' or PersonaNatural.apePaternoPersona like '%"+criterio+"%' ").getResultList();
    }

     public List<GarantiaPrendaria> findPrendariasByDNI(String criterio)
    {
        return em.createQuery("select object(o) from GarantiaPrendaria as o inner join o.cliente PersonaNatural where PersonaNatural.dniPersona ='"+criterio+"' ").getResultList();
    }

    public void retasarPrendaria (String id, String tasacion)
    {
        em.createQuery("UPDATE GarantiaPrendaria set valortasacion = " +tasacion+ " WHERE idGarantia = " + id);

    }
    //Avales

    public Aval findAval(Object id) {
        return em.find(Aval.class, id);
    }

    public List<Aval> findAllAvales() {
        return em.createQuery("select object(o) from Aval as o").getResultList();
    }
    public List<Aval> findAvalByPropietario(String criterioAval) {
        return em.createQuery("select object(o) from Aval o where o.nombre like '%"+criterioAval+"%'").getResultList();
    }
    //Garantias Hipotecarias

    public List<GarantiaHipotecaria> findAllHipotecarias(){
        return em.createQuery("select object(o) from GarantiaHipotecaria as o").getResultList();
    }

    //Garantias Warrant

    public List<GarantiaWarrant> findAllWarrant() {
        return em.createQuery("select object(o) from GarantiaWarrant as o").getResultList();
    }

    //Garantias Pignoraticias

    public List<GarantiaPignoraticia> findAllPignoraticias() {
        return em.createQuery("select object(o) from GarantiaPignoraticia as o").getResultList();
    }
}
